#include "stdafx.h"
#include <assert.h>

#include "RenderArea.h"
#include <gl/gl.h>

CRenderArea::CRenderArea( ) :
	m_HWND( NULL ),
	m_HDC( NULL ),
	m_HGLRC( NULL )
{
}

CRenderArea::~CRenderArea( )
{
}

void CRenderArea::Initialize( HWND inHWND )
{
	m_HWND = inHWND;
	assert( m_HWND );

	m_HDC = ::GetDC( m_HWND );
	assert( m_HDC );
	
	PIXELFORMATDESCRIPTOR thePixelFormatDescriptor;

	// set the pixel format for the DC
	ZeroMemory( &thePixelFormatDescriptor, sizeof( thePixelFormatDescriptor ) );
	thePixelFormatDescriptor.nSize = sizeof( thePixelFormatDescriptor );
	thePixelFormatDescriptor.nVersion = 1;
	thePixelFormatDescriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	thePixelFormatDescriptor.iPixelType = PFD_TYPE_RGBA;
	thePixelFormatDescriptor.cColorBits = 24;
	thePixelFormatDescriptor.cDepthBits = 16;
	thePixelFormatDescriptor.iLayerType = PFD_MAIN_PLANE;

	int thePixelFormat = ::ChoosePixelFormat( m_HDC, &thePixelFormatDescriptor );
	::SetPixelFormat( m_HDC, thePixelFormat, &thePixelFormatDescriptor );

	m_HGLRC = wglCreateContext( m_HDC );
	assert( m_HGLRC );

	wglMakeCurrent( m_HDC, m_HGLRC );
}

void CRenderArea::Render( )
{
	glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
	glClear( GL_COLOR_BUFFER_BIT );
	
	glPushMatrix( );
	
	glBegin( GL_TRIANGLES );
	glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( 0.0f, 1.0f );
	glColor3f( 0.0f, 1.0f, 0.0f ); glVertex2f( 0.87f, -0.5f );
	glColor3f( 0.0f, 0.0f, 1.0f ); glVertex2f( -0.87f, -0.5f );
	glEnd( );
	
	glPopMatrix( );
	
	SwapBuffers( m_HDC );
}

void CRenderArea::Destroy( )
{
	wglMakeCurrent( NULL, NULL );
	wglDeleteContext( m_HGLRC );
	ReleaseDC( m_HWND, m_HDC );
}